/**
 1:模块目录分类
 2:配置文件，包括路径，当前全局模块
 3:同级组件之间，父子级组件之间通信的问题
 */

import director from 'director';

console.log('run app');

var App = React.createClass({
    
    getInitialState () {
        return {
            route:'/',
            type:'whole',
            obj:{},
        };
    },

    componentDidMount () {
        var tthis = this,   
             routes = {
               '/:module': function(module) {tthis.routerHandle(module,'whole',{})},
               '/:module/:type': function(id,type) {tthis.routerHandle(module,type,{})},
               '/:module/:type/:obj': function(id,type,obj) {console.log(id);tthis.routerHandle(module,type,obj)},
               '/.*': function(module) {tthis.routerHandle('home','whole',{})},
             };
        var router = director.Router(routes);
        router.init();
    },

    routerHandle : function(module,type,obj) {     
        this.setState({
            route: module,
            type:type,
            obj:obj        
        });
    },

    getModuleName : function() {
        var moduleName,module = this.state.route;
        if (module == '/') {
            moduleName = 'Home';
        } else {
            var moduleA = module.substr(0,1).toUpperCase(),
                moduleB = module.substr(1);
            moduleName = moduleA+moduleB;
        }
        return moduleName;
    },

    render : function() {

        var moduleName = this.getModuleName(),
             Nav = require('./js/components/common/Nav.react'),
             Child = require('./js/components/'+moduleName+'/'+moduleName+'.react');
      
        return (
            <div className="box">
                 <Nav/>
                 <Child/>
            </div>
        )
      }
});

/*
<div>
<ul>
   <li><a href="#/home">home</a></li>
   <li><a href="#/about">About</a></li>
   <li><a href="#/inbox">Inbox</a></li>
   <li><a href="#/todo">Todo</a></li>
</ul>
</div>
*/
React.render(<App />, document.getElementById('react'));